package com.shuoyue.ycgk.ui.mycourse;

import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Course;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface MyCourseContract {
    interface View extends BaseView {
        void getMyCourseSuc(List<Course> courses);

        void getAllCourseSuc(ListWithPage<Course> courses);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getMyCourse(Integer type) {
            apply(model.getMyCourse(type))
                    .subscribe(new ApiSubscriber<Optional<List<Course>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<Course>> listOptional) {
                            super.onNext(listOptional);
                            mView.getMyCourseSuc(listOptional.getIncludeNull());
                        }
                    });
        }

        public void getMyInvalidateCourse() {
            apply(model.getMyInvalidateCourse())
                    .subscribe(new ApiSubscriber<Optional<List<Course>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<Course>> listOptional) {
                            super.onNext(listOptional);
                            mView.getMyCourseSuc(listOptional.getIncludeNull());
                        }
                    });
        }

        public void getAllCourse(Integer type, int moduleId, int page) {
            apply(model.getAllCourse(type, moduleId, page))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<Course>>>(mView, this) {
                        @Override
                        public void onNext(Optional<ListWithPage<Course>> listOptional) {
                            super.onNext(listOptional);
                            mView.getAllCourseSuc(listOptional.getIncludeNull());
                        }
                    });
        }
    }

    class Model {
        Observable<BaseResult<List<Course>>> getMyCourse(Integer type) {
            return RetrofitClient.getInstance().getApi().getMyCourse();
        }

        Observable<BaseResult<List<Course>>> getMyInvalidateCourse() {
            return RetrofitClient.getInstance().getApi().getMyInvalidateCourse();
        }

        Observable<BaseResult<ListWithPage<Course>>> getAllCourse(Integer type, int moduleId, int n) {
            return RetrofitClient.getInstance().getApi().getAllCourse(type, moduleId, n);
        }
    }
}
